package com.wf.day_0613.test3;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Demo {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition c1 = lock.newCondition();
        Condition c2 = lock.newCondition();
        Condition c3 = lock.newCondition();
      //  Condition c4 = lock.newCondition();
        ArrayList<Integer> list = new ArrayList<>();


        Thread a = new Thread(() -> {
            int i = 0;
            while (true) {
                try {
                    lock.lock();
                    if (i > 10) {
                        break;
                    } else {
                        System.out.print("A");
                        i++;
                        c1.await();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    c2.signal();
                    lock.unlock();
                }
            }
        }, "A");

        Thread b = new Thread(() -> {
            int i = 0;
            while (true) {
                try {
                    Thread.sleep(100);
                    lock.lock();
                    if (i > 10) {
                        break;
                    } else {
                        System.out.print("B");
                        i++;
                        c2.await();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    c3.signal();
                    lock.unlock();
                }
            }
        }, "B");

        Thread c = new Thread(() -> {
            int i = 0;
            while (true) {
                try {
                    Thread.sleep(200);
                    lock.lock();
                    if (i > 10) {
                        break;
                    } else {
                        System.out.print("C");
                        i++;
                        c3.await();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {

                    lock.unlock();
                }
            }
        }, "C");
        Thread d = new Thread(() -> {
            int i = 0;
            while (true) {
                try {
                    Thread.sleep(300);
                    lock.lock();
                    if (i > 10) {
                        break;
                    } else {
                        System.out.println("D");
                        i++;

                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    c1.signal();
                    lock.unlock();
                }
            }
        }, "C");
        b.start();
        c.start();
        a.start();
        d.start();

    }
}
